<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #shop-car li {
            border: 1px dotted red;
            margin: 20px 0;
            padding: 20px;
        }

        .container {
            width: 400px;
            padding: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul id="shop-car">
            <!-- <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg"
                    alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中：选中吧OR不选中</div>
            </li> -->
        </ul>
        <p id="totalnum">总数量：<span>0</span></p>
    </div>

    <script src="./jquery.min.js"></script>
    <script>

        let products = [{
                goods_name: '小米10-1',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '500',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 10,
                goods_is_checked: false
            },
            {
                goods_name: '小米10-2',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '510',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 50,
                goods_is_checked: false
            }
        ]

        let shopCar = document.querySelector('#shop-car');
        let p = document.querySelector('p');
        let sc_li = '';
        for (let i = 0;i < products.length;i++){
            sc_li += `<li>\
                <input type="checkbox"${products.goods_is_checked ? "checked" : ""}>\
                <div>商品名称:${products[i].goods_name}</div>\
                <img src="${products[i].goods_img}"\
                    alt="">\
                <div>商品价格:${products[i].goods_price}</div>\
                <div>购买数量:${products[i].goods_num}</div>\
                <div>商品规格: ${products[i].goods_attr}</div>\
                <div class="ck">是否选中：${products.goods_is_checked ? "选中" : "未选中"}</div>\
            </li>`;
        }
        shopCar.innerHTML = sc_li;
        
        sun()

        $(':checkbox').on('click',function(){
            $(this).siblings('.ck').text(`是否选中：${$(this).prop('checked') ? "选中" : "未选中"}`);
            products[$(this).parent().index()].goods_is_checked = $(this).prop('checked') ? true : false;
            sun()
        })
        

        function sun(){
            let count = 0;
            let res = products.filter(function(v){
                return v.goods_is_checked == true;
            })
            let res1 = res.map(function(v){
                return v.goods_num
            })
            res1.forEach(function(v){
                count = count + v;
            })
            $('span').text(count)
        }
    </script>
</body>

</html>